﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[ParseChildren(true)]
	[Obsolete("Use embeded script instead.", true)]
	public class MySwitch : Control, INamingContainer {
		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case0 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case1 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case2 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case3 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case4 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case5 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case6 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case7 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case8 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case9 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case10 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case11 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case12 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case13 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case14 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Multiple)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Case15 { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Otherwise { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Bindable(BindableSupport.Yes, BindingDirection.OneWay)]
		[DefaultValue(false)]
		public object Test { get; set; }

		private object m_dataItem = null;

		private bool m_requiresDataBinding = false;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer) {
			RenderChildren(writer);
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void CreateChildControls() {
			Controls.Clear();

			var ct = Convert.ToString(Test);
			bool flag;
			int bt;

			if (!string.IsNullOrWhiteSpace(ct)) {
				ct = ct.Trim().ToLower();
				flag = int.TryParse(ct, out bt);
			} else {
				bt = 0;
				flag = false;
			}

			var itc = new ItemTemplateContent(m_dataItem, 0, 0);

			if (!flag) {
				if (Otherwise != null)
					Otherwise.InstantiateIn(itc);
			} else {
				if (bt == 0 && Case0 != null)
					Case0.InstantiateIn(itc);
				else if (bt == 1 && Case1 != null)
					Case1.InstantiateIn(itc);
				else if (bt == 2 && Case2 != null)
					Case2.InstantiateIn(itc);
				else if (bt == 3 && Case3 != null)
					Case3.InstantiateIn(itc);
				else if (bt == 4 && Case4 != null)
					Case4.InstantiateIn(itc);
				else if (bt == 5 && Case5 != null)
					Case5.InstantiateIn(itc);
				else if (bt == 6 && Case6 != null)
					Case6.InstantiateIn(itc);
				else if (bt == 7 && Case7 != null)
					Case7.InstantiateIn(itc);
				else if (bt == 8 && Case8 != null)
					Case8.InstantiateIn(itc);
				else if (bt == 9 && Case9 != null)
					Case9.InstantiateIn(itc);
				else if (bt == 10 && Case10 != null)
					Case10.InstantiateIn(itc);
				else if (bt == 11 && Case11 != null)
					Case11.InstantiateIn(itc);
				else if (bt == 12 && Case12 != null)
					Case12.InstantiateIn(itc);
				else if (bt == 13 && Case13 != null)
					Case13.InstantiateIn(itc);
				else if (bt == 14 && Case14 != null)
					Case14.InstantiateIn(itc);
				else if (bt == 15 && Case15 != null)
					Case15.InstantiateIn(itc);
				else if (Otherwise != null)
					Otherwise.InstantiateIn(itc);
			}

			Controls.Add(itc);

			if (m_requiresDataBinding) {
				itc.DataBind();
			}

			ChildControlsCreated = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnDataBinding(EventArgs e) {
			m_dataItem = Page.GetDataItem();
			m_requiresDataBinding = true;

			base.OnDataBinding(e);
		}
	} // end of IIf.
}
